/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2007
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DNNContentBuilder.UI.WebControls
{
	/// <summary>
	/// This class is used to render the pager at design time.
	/// </summary>
	/// <remarks>You should not use this class in your projects.</remarks>
	public class PagerBuilderDesigner : System.Web.UI.Design.ControlDesigner
	{
		#region Implementation
		public override string GetDesignTimeHtml() 
		{
			LinkButton previousPageButton;
			LinkButton firstPageButton;
			LinkButton previousGroupButton;
			LinkButton gotoPageButton;
			LinkButton nextGroupButton;
			LinkButton lastPageButton;
			LinkButton nextPageButton;

			StringWriter sw		  = new StringWriter();
			HtmlTextWriter writer = new HtmlTextWriter(sw);
			PagerBuilder pager	  = this.Component as PagerBuilder;
			
			Table container		= new Table();
			TableRow panel		= new TableRow();
			TableCell infoPanel	= new TableCell();
			TableCell navPanel	= new TableCell();

			panel.Cells.Add(infoPanel);
			panel.Cells.Add(navPanel);
			panel.CssClass = pager.PanelCssClass;
			
			container.Rows.Add(panel);
			container.ApplyStyle(pager.PagerStyle);
			
			container.Width = pager.Width;
			container.Height = pager.Height;
			container.HorizontalAlign = pager.HorizontalAlign;
			container.CellPadding = pager.CellPadding;
			container.CellSpacing = pager.CellSpacing;
			
			
			infoPanel.ApplyStyle(pager.PagerStyle);
			infoPanel.HorizontalAlign = pager.InfoPanelHorizontalAlign;
			infoPanel.VerticalAlign = pager.InfoPanelVerticalAlign;
			infoPanel.Visible = pager.InfoPanelDisabled == false;
			
			navPanel.ApplyStyle(pager.PagerStyle);
			navPanel.HorizontalAlign = pager.NavPanelHorizontalAlign;
			navPanel.VerticalAlign = pager.NavPanelVerticalAlign;
			
			Label status = new Label();
			status.Text = pager.BuildInfo(true);
			
			infoPanel.Controls.Add(status);
			
			previousPageButton = new LinkButton();
			previousPageButton.Enabled = false;
			previousPageButton.Text = pager.PreviousText;
			
			navPanel.Controls.Add(previousPageButton);
			navPanel.Controls.Add(new LiteralControl(pager.NavSeparator));
			
			if (!(pager.FirstLastDisabled)) 
			{
				firstPageButton = new LinkButton();
				firstPageButton.Enabled = false;
				firstPageButton.Text = pager.FirstText;
				
				navPanel.Controls.Add(firstPageButton);
				navPanel.Controls.Add(new LiteralControl(pager.NavSeparator));
			}

			if (!pager.PageGroupDisabled) 
			{
				previousGroupButton = new LinkButton();
				previousGroupButton.Enabled = false;
				previousGroupButton.Text = "&lt;";
				
				navPanel.Controls.Add(previousGroupButton);
				navPanel.Controls.Add(new LiteralControl("["));
				
				int currentIndex = 1;
				
				while (currentIndex <= 10) 
				{
					gotoPageButton = new LinkButton();
					
					if (currentIndex != 1)
					{
						// TODO: goto i-1;
					}

					gotoPageButton.Text = "<b>" + currentIndex.ToString() + "</b>";
					navPanel.Controls.Add(gotoPageButton);

					if (currentIndex != 10)
					{
						navPanel.Controls.Add(new LiteralControl("&nbsp;"));
					}
					currentIndex++;
				}
				
				nextGroupButton = new LinkButton();
				nextGroupButton.Text = "&gt;";
				
				navPanel.Controls.Add(new LiteralControl("]"));
				navPanel.Controls.Add(nextGroupButton);
				navPanel.Controls.Add(new LiteralControl(pager.NavSeparator));
			}

			if (!pager.FirstLastDisabled) 
			{
				lastPageButton = new LinkButton();
				lastPageButton.Text = pager.LastText;
				navPanel.Controls.Add(lastPageButton);
			}
			
			navPanel.Controls.Add(new LiteralControl(pager.NavSeparator));
			nextPageButton = new LinkButton();
			nextPageButton.Text = pager.NextText;
			
			navPanel.Controls.Add(nextPageButton);
			container.RenderControl(writer);
			
			return sw.ToString();
		}

		protected override string GetEmptyDesignTimeHtml() 
		{
			return this.CreatePlaceHolderDesignTimeHtml("this should be never displayed.");
		}
		#endregion
	}
}
